import sys
import re

def count_characters(text):
    """统计字符数（包括空格和换行符）"""
    return len(text)

def count_words(text):
    """统计单词数（以空格、标点或换行分隔的连续字母数字序列）"""
    # 使用正则表达式匹配单词（连续的字母数字字符）
    words = re.findall(r'\b[a-zA-Z]+\b', text)
    return len(words)

def count_sentences(text):
    """统计句子数（以. ! ?结尾的为一个句子）"""
    # 使用正则表达式匹配句子结尾
    sentences = re.split(r'[.!?]+', text)
    # 过滤空字符串
    sentences = [s.strip() for s in sentences if s.strip()]
    return len(sentences)

def main():
    # 检查参数数量
    if len(sys.argv) != 3:
        print("用法: wc.exe [参数] [文件名]")
        print("参数:")
        print("  -c  统计字符数")
        print("  -w  统计单词数")
        print("  -s  统计句子数")
        return
    
    option = sys.argv[1]
    filename = sys.argv[2]
    
    # 检查参数有效性
    if option not in ['-c', '-w', '-s']:
        print(f"错误: 无效的参数 '{option}'")
        print("可用参数: -c, -w, -s")
        return
    
    try:
        # 读取文件
        with open(filename, 'r', encoding='utf-8') as file:
            text = file.read()
        
        # 根据参数进行统计
        if option == '-c':
            count = count_characters(text)
            print(f"字符数: {count}")
        elif option == '-w':
            count = count_words(text)
            print(f"单词数: {count}")
        elif option == '-s':
            count = count_sentences(text)
            print(f"句子数: {count}")
            
    except FileNotFoundError:
        print(f"错误: 文件 '{filename}' 未找到")
    except Exception as e:
        print(f"错误: 读取文件时发生错误 - {e}")

if __name__ == "__main__":
    main()